package com.itheima.kehuduanfuwuqi;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器ServerSocket对象
        ServerSocket ss = new ServerSocket(10010);
        //创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                10,
                20,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),//阻塞队列
                Executors.defaultThreadFactory(),//创建线程方式
                new ThreadPoolExecutor.AbortPolicy());//任务拒绝策略
        while (true) {
            //创建Socket对象
            Socket accept = ss.accept();
            //创建多线程对象
            ThreadSocket ts = new ThreadSocket(accept);
//            new Thread(ts).start();
            pool.submit(ts);
        }
//        ss.close();
    }
}
